#include "splitimgtopt.h"
splitImgToPt::splitImgToPt(QObject *parent) : QObject(parent)
{
}

////获取交点
//int twoLine1(std::vector<cv::Vec4i>& validLines,double& a,double& b)
//{
//cv::Vec4i line1 = validLines[0];
//cv::Vec4i line2 = validLines[1];
//double a1 = double(line1[3] - line1[1]) / double(line1[2] - line1[0]);//斜率
//double b1 = double(line1[2] * line1[1] - line1[3] * line1[0]) / double(line1[2] - line1[0]);//截距
//double a2 = double(line2[3] - line2[1]) / double(line2[2] - line2[0]);
//double b2 = double(line2[2] * line2[1] - line2[3] * line2[0]) / double(line2[2] - line2[0]);
//
////根据两个直线的斜率和截距计算出交点的X坐标
//int nJointX = int((b1 - b2) / (a2 - a1));
//a = a1;
//b = b1;
//return nJointX;
//}
//角度转化成弧度，再用tan计算斜率
int splitImgToPt::splitImg(string imgPath,const int AngleL, const int AngleR)
{
    angleL = AngleL * CV_PI / 180;//弧度
    angleR = AngleR * CV_PI / 180;//弧度
    std::cout << tan(angleL) << std::endl;
    std::cout << tan(angleR) << std::endl;
    // 读取源图片
    cv::Mat srcImage = cv::imread(imgPath);
    if (srcImage.empty()) {
        std::cerr << "Error: Could not read source image." << std::endl;
        return -1;
    }
    std::cout << srcImage.cols << std::endl;
    std::cout << srcImage.rows << std::endl;
    // 创建一个与源图片大小相同的空白图片
    cv::Mat destImage(srcImage.rows, srcImage.cols, srcImage.type(), cv::Scalar(0));
    double aLfet = -tan(angleL);//斜率
    double bLeft = double(Yoffset - aLfet * XoffsetL);//截距
    double aRight = -tan(angleR);
    double bRight = double(Yoffset - aRight * XoffsetR);
    std::vector<double> kb2{ aLfet,bLeft };
    int nJointXLeft = GetJointX(line1, kb2);
    std::vector<double> kb2_2{ aRight,bRight };
    int nJointXRight = GetJointX(line2, kb2_2);
    // 遍历源图片的每个像素，并将其拷贝到目标图片上
    for (int i = 0; i < srcImage.cols; ++i) {
        for (int j = 0; j < srcImage.rows; ++j) {
            if (i >= nJointXLeft && i < srcImage.cols / 2)
            {
                int nYloc = int(aLfet * i + bLeft);
                if ((j <= nYloc) && (i < destImage.cols) && (j < destImage.rows))
                {
                    // 获取源图片的像素值
                    cv::Vec3b pixel = srcImage.at<cv::Vec3b>(j, i);
                    // 将像素值设置到目标图片上
                    destImage.at<cv::Vec3b>(j, i) = pixel;
                }
            }
            else if (i >= srcImage.cols / 2 && i <= nJointXRight)
            {
                int nYloc = int(aRight * i + bRight);
                if ((j <= nYloc) && (i < destImage.cols) && (j < destImage.rows))
                {
                    // 获取源图片的像素值
                    cv::Vec3b pixel = srcImage.at<cv::Vec3b>(j, i);
                    // 将像素值设置到目标图片上
                    destImage.at<cv::Vec3b>(j, i) = pixel;
                }
            }
        }
    }
    //计算五个角点
    Point top(line2[0],line2[1]);
    Point rightTop(nJointXRight, nJointXRight*aRight+bRight);
    Point rightBottom(XoffsetR, Yoffset);
    Point leftBottom(XoffsetL, Yoffset);
    Point leftTop(nJointXLeft, nJointXLeft*aLfet+bLeft);
    std::vector<Point> ptarr = { top,rightTop,rightBottom,leftBottom,leftTop };
    addfFivePoint(idx++, ptarr);
    std::string id = to_string(idx);
    std::string splitLast = "E:\\jointImg\\src2Pic\\split-lastArr\\split-last" + id + ".jpg";
    cv::imwrite(splitLast, destImage);
    return 0;
}
/*************固定角度裁剪***************/
//double aLfet ;//斜率
//double bLeft;//截距
//double aRight;
//double bRight;
//std::vector<cv::Vec4i> validLines1;
//validLines1.push_back({ 0,0,550,1079 });
//validLines1.push_back({ 0,1079,1920 / 2,0 });
//int nJointXLeft = twoLine(validLines1, aLfet,bLeft);
//
//std::vector<cv::Vec4i> validLines2;
//validLines2.push_back({1088,1079,1919,0 });
//validLines2.push_back({ 1920/2,0,1919,1079 });
//int nJointXRight = twoLine(validLines2,aRight,bRight);
//
//// 遍历源图片的每个像素，并将其拷贝到目标图片上
//for (int i = 0; i < srcImage.cols; ++i) {
//for (int j = 0; j < srcImage.rows; ++j) {
//if (i>=nJointXLeft && i< srcImage.cols/2)
//{
//int nYloc = int(aLfet * i + bLeft);
//if ((j <= nYloc)&&(i < destImage.cols) && (j < destImage.rows))
//{
//// 获取源图片的像素值
//cv::Vec3b pixel = srcImage.at<cv::Vec3b>(j,i);
//// 将像素值设置到目标图片上
//destImage.at<cv::Vec3b>(j, i) = pixel;
//}
//}
//
//else if (i >= srcImage.cols / 2 && i <= nJointXRight )
//{
//int nYloc = int(aRight * i + bRight);
//if ((j <= nYloc) && (i < destImage.cols) && (j < destImage.rows))
//{
//// 获取源图片的像素值
//cv::Vec3b pixel = srcImage.at<cv::Vec3b>(j, i);
//// 将像素值设置到目标图片上
//destImage.at<cv::Vec3b>(j, i) = pixel;
//}
//}
//}
//}
void splitImgToPt::addfFivePoint(int idx, std::vector<Point>& ptarr)
{
    if (idx < 6)
        pointArr[idx] = ptarr;
    else
        cout << "insert err cross the border" << endl;
}
void splitImgToPt::twoPointToStandard(double x1, double y1, double x2, double y2, double &a, double &b, double &c)
{
    a = y2 - y1;
    b = x1 - x2;
    c = x2 * y1 - x1 * y2;
}
void splitImgToPt::slopeInterceptToStandard(double k, double b, double &a, double &b1, double &c)
{
    a = k;
    b1 = -1;
    c = b;
}
bool splitImgToPt::getIntersection(double a1, double b1, double c1, double a2, double b2, double c2, Point2d &pt)
{
    double d = a1 * b2 - a2 * b1;
    if (d == 0) {
        return false;
    }
    pt.x = (b1 * c2 - b2 * c1) / d;
    pt.y = (a2 * c1 - a1 * c2) / d;
    return true;
}
double splitImgToPt::GetJointX(Vec4d line, std::vector<double> kb2)
{
    // 两点式直线方程
    double x1 = line[0], y1 = line[1], x2 = line[2], y2 = line[3];
    double a1, b1, c1;
    twoPointToStandard(x1, y1, x2, y2, a1, b1, c1);
    // 斜截式直线方程
    double k = kb2[0], b = kb2[1];
    double a2, b2, c2;
    slopeInterceptToStandard(k, b, a2, b2, c2);
    // 求解交点
    Point2d pt;
    if (getIntersection(a1, b1, c1, a2, b2, c2, pt)) {
        cout << "交点坐标为：" << pt << endl;
    }
    else {
        //cout << "两条直线平行！" << endl;
    }
    return pt.x;
}
